package com.lck.blogmanager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lck.blogmanager.entity.BlogCommentEntity;
import com.lck.blogmanager.mapper.BlogCommentMapper;
import com.lck.blogmanager.req.AddBlogCommentReq;
import com.lck.blogmanager.req.PageBlogCommentReq;
import com.lck.blogmanager.req.UserDeleteBlogCommentReq;
import com.lck.blogmanager.res.UserBlogCommentPageRes;
import com.lck.blogmanager.service.BlogCommentService;
import com.lck.blogmanager.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/4/7 17:18
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogCommentEntity> implements BlogCommentService {
    @Override
    public Integer addBlogComment(AddBlogCommentReq req) {

        BlogCommentEntity comment = new BlogCommentEntity();
        if(req.getTargetId()!=null){
            BlogCommentEntity targetCommont = this.getById(req.getTargetId());
            comment.setTargetId(targetCommont.getUserId());
        }
        comment.setUserId(SecurityUtils.getUserId());
        comment.setBody(req.getBody());
        comment.setCreateTime(LocalDateTime.now());
        comment.setBlogId(req.getBlogId());
        this.getBaseMapper().insert(comment);
        return comment.getId();
    }

    @Override
    public Page<UserBlogCommentPageRes> pageBlogComment(PageBlogCommentReq req) {
        Page page = new Page(req.getPageNum(), req.getPageSize());
        Page<UserBlogCommentPageRes> info = this.getBaseMapper().getComments(req,page);
        return info;
    }

    @Override
    public void deleteComment(UserDeleteBlogCommentReq req) {
        this.getBaseMapper().deleteById(req.getId());
    }
}
